#include "stdafx.h"
#include "mime.h"

namespace clib {

    std::tuple<std::string, std::string> mime_list[] = {
        std::make_tuple("acx", "application/internet-property-stream"),
        std::make_tuple("ai", "application/postscript"),
        std::make_tuple("aif", "audio/x-aiff"),
        std::make_tuple("aifc", "audio/x-aiff"),
        std::make_tuple("aiff", "audio/x-aiff"),
        std::make_tuple("asf", "video/x-ms-asf"),
        std::make_tuple("asr", "video/x-ms-asf"),
        std::make_tuple("asx", "video/x-ms-asf"),
        std::make_tuple("au", "audio/basic"),
        std::make_tuple("avi", "video/x-msvideo"),
        std::make_tuple("axs", "application/olescript"),
        std::make_tuple("bas", "text/plain"),
        std::make_tuple("bcpio", "application/x-bcpio"),
        std::make_tuple("bin", "application/octet-stream"),
        std::make_tuple("bmp", "image/bmp"),
        std::make_tuple("c", "text/plain"),
        std::make_tuple("cat", "application/vnd.ms-pkiseccat"),
        std::make_tuple("cdf", "application/x-cdf"),
        std::make_tuple("cer", "application/x-x509-ca-cert"),
        std::make_tuple("class", "application/octet-stream"),
        std::make_tuple("clp", "application/x-msclip"),
        std::make_tuple("cmx", "image/x-cmx"),
        std::make_tuple("cod", "image/cis-cod"),
        std::make_tuple("cpio", "application/x-cpio"),
        std::make_tuple("cpp", "text/plain"),
        std::make_tuple("crd", "application/x-mscardfile"),
        std::make_tuple("crl", "application/pkix-crl"),
        std::make_tuple("crt", "application/x-x509-ca-cert"),
        std::make_tuple("csh", "application/x-csh"),
        std::make_tuple("css", "text/css"),
        std::make_tuple("dcr", "application/x-director"),
        std::make_tuple("der", "application/x-x509-ca-cert"),
        std::make_tuple("dir", "application/x-director"),
        std::make_tuple("dll", "application/x-msdownload"),
        std::make_tuple("dms", "application/octet-stream"),
        std::make_tuple("doc", "application/msword"),
        std::make_tuple("dot", "application/msword"),
        std::make_tuple("dvi", "application/x-dvi"),
        std::make_tuple("dxr", "application/x-director"),
        std::make_tuple("eps", "application/postscript"),
        std::make_tuple("etx", "text/x-setext"),
        std::make_tuple("evy", "application/envoy"),
        std::make_tuple("exe", "application/octet-stream"),
        std::make_tuple("fif", "application/fractals"),
        std::make_tuple("flr", "x-world/x-vrml"),
        std::make_tuple("gif", "image/gif"),
        std::make_tuple("gtar", "application/x-gtar"),
        std::make_tuple("gz", "application/x-gzip"),
        std::make_tuple("h", "text/plain"),
        std::make_tuple("hdf", "application/x-hdf"),
        std::make_tuple("hlp", "application/winhlp"),
        std::make_tuple("hqx", "application/mac-binhex40"),
        std::make_tuple("hta", "application/hta"),
        std::make_tuple("htc", "text/x-component"),
        std::make_tuple("htm", "text/html;charset=utf-8"),
        std::make_tuple("html", "text/html;charset=utf-8"),
        std::make_tuple("htt", "text/webviewhtml"),
        std::make_tuple("ico", "image/x-icon"),
        std::make_tuple("ief", "image/ief"),
        std::make_tuple("iii", "application/x-iphone"),
        std::make_tuple("ins", "application/x-internet-signup"),
        std::make_tuple("isp", "application/x-internet-signup"),
        std::make_tuple("jfif", "image/pipeg"),
        std::make_tuple("jpe", "image/jpeg"),
        std::make_tuple("jpeg", "image/jpeg"),
        std::make_tuple("jpg", "image/jpeg"),
        std::make_tuple("js", "application/x-javascript"),
        std::make_tuple("latex", "application/x-latex"),
        std::make_tuple("lha", "application/octet-stream"),
        std::make_tuple("lsf", "video/x-la-asf"),
        std::make_tuple("lsx", "video/x-la-asf"),
        std::make_tuple("lzh", "application/octet-stream"),
        std::make_tuple("m13", "application/x-msmediaview"),
        std::make_tuple("m14", "application/x-msmediaview"),
        std::make_tuple("m3u", "audio/x-mpegurl"),
        std::make_tuple("man", "application/x-troff-man"),
        std::make_tuple("mdb", "application/x-msaccess"),
        std::make_tuple("me", "application/x-troff-me"),
        std::make_tuple("mht", "message/rfc822"),
        std::make_tuple("mhtml", "message/rfc822"),
        std::make_tuple("mid", "audio/mid"),
        std::make_tuple("mny", "application/x-msmoney"),
        std::make_tuple("mov", "video/quicktime"),
        std::make_tuple("movie", "video/x-sgi-movie"),
        std::make_tuple("mp2", "video/mpeg"),
        std::make_tuple("mp3", "audio/mpeg"),
        std::make_tuple("mpa", "video/mpeg"),
        std::make_tuple("mpe", "video/mpeg"),
        std::make_tuple("mpeg", "video/mpeg"),
        std::make_tuple("mpg", "video/mpeg"),
        std::make_tuple("mpp", "application/vnd.ms-project"),
        std::make_tuple("mpv2", "video/mpeg"),
        std::make_tuple("ms", "application/x-troff-ms"),
        std::make_tuple("mvb", "application/x-msmediaview"),
        std::make_tuple("nws", "message/rfc822"),
        std::make_tuple("oda", "application/oda"),
        std::make_tuple("p10", "application/pkcs10"),
        std::make_tuple("p12", "application/x-pkcs12"),
        std::make_tuple("p7b", "application/x-pkcs7-certificates"),
        std::make_tuple("p7c", "application/x-pkcs7-mime"),
        std::make_tuple("p7m", "application/x-pkcs7-mime"),
        std::make_tuple("p7r", "application/x-pkcs7-certreqresp"),
        std::make_tuple("p7s", "application/x-pkcs7-signature"),
        std::make_tuple("pbm", "image/x-portable-bitmap"),
        std::make_tuple("pdf", "application/pdf"),
        std::make_tuple("pfx", "application/x-pkcs12"),
        std::make_tuple("pgm", "image/x-portable-graymap"),
        std::make_tuple("pko", "application/ynd.ms-pkipko"),
        std::make_tuple("pma", "application/x-perfmon"),
        std::make_tuple("pmc", "application/x-perfmon"),
        std::make_tuple("pml", "application/x-perfmon"),
        std::make_tuple("pmr", "application/x-perfmon"),
        std::make_tuple("pmw", "application/x-perfmon"),
        std::make_tuple("pnm", "image/x-portable-anymap"),
        std::make_tuple("pot,", "application/vnd.ms-powerpoint"),
        std::make_tuple("ppm", "image/x-portable-pixmap"),
        std::make_tuple("pps", "application/vnd.ms-powerpoint"),
        std::make_tuple("ppt", "application/vnd.ms-powerpoint"),
        std::make_tuple("prf", "application/pics-rules"),
        std::make_tuple("ps", "application/postscript"),
        std::make_tuple("pub", "application/x-mspublisher"),
        std::make_tuple("qt", "video/quicktime"),
        std::make_tuple("ra", "audio/x-pn-realaudio"),
        std::make_tuple("ram", "audio/x-pn-realaudio"),
        std::make_tuple("ras", "image/x-cmu-raster"),
        std::make_tuple("rgb", "image/x-rgb"),
        std::make_tuple("rmi", "audio/mid"),
        std::make_tuple("roff", "application/x-troff"),
        std::make_tuple("rtf", "application/rtf"),
        std::make_tuple("rtx", "text/richtext"),
        std::make_tuple("scd", "application/x-msschedule"),
        std::make_tuple("sct", "text/scriptlet"),
        std::make_tuple("setpay", "application/set-payment-initiation"),
        std::make_tuple("setreg", "application/set-registration-initiation"),
        std::make_tuple("sh", "application/x-sh"),
        std::make_tuple("shar", "application/x-shar"),
        std::make_tuple("sit", "application/x-stuffit"),
        std::make_tuple("snd", "audio/basic"),
        std::make_tuple("spc", "application/x-pkcs7-certificates"),
        std::make_tuple("spl", "application/futuresplash"),
        std::make_tuple("src", "application/x-wais-source"),
        std::make_tuple("sst", "application/vnd.ms-pkicertstore"),
        std::make_tuple("stl", "application/vnd.ms-pkistl"),
        std::make_tuple("stm", "text/html"),
        std::make_tuple("svg", "image/svg+xml"),
        std::make_tuple("sv4cpio", "application/x-sv4cpio"),
        std::make_tuple("sv4crc", "application/x-sv4crc"),
        std::make_tuple("swf", "application/x-shockwave-flash"),
        std::make_tuple("t", "application/x-troff"),
        std::make_tuple("tar", "application/x-tar"),
        std::make_tuple("tcl", "application/x-tcl"),
        std::make_tuple("tex", "application/x-tex"),
        std::make_tuple("texi", "application/x-texinfo"),
        std::make_tuple("texinfo", "application/x-texinfo"),
        std::make_tuple("tgz", "application/x-compressed"),
        std::make_tuple("tif", "image/tiff"),
        std::make_tuple("tiff", "image/tiff"),
        std::make_tuple("tr", "application/x-troff"),
        std::make_tuple("trm", "application/x-msterminal"),
        std::make_tuple("tsv", "text/tab-separated-values"),
        std::make_tuple("txt", "text/plain"),
        std::make_tuple("uls", "text/iuls"),
        std::make_tuple("ustar", "application/x-ustar"),
        std::make_tuple("vcf", "text/x-vcard"),
        std::make_tuple("vrml", "x-world/x-vrml"),
        std::make_tuple("wav", "audio/x-wav"),
        std::make_tuple("wcm", "application/vnd.ms-works"),
        std::make_tuple("wdb", "application/vnd.ms-works"),
        std::make_tuple("wks", "application/vnd.ms-works"),
        std::make_tuple("wmf", "application/x-msmetafile"),
        std::make_tuple("wps", "application/vnd.ms-works"),
        std::make_tuple("wri", "application/x-mswrite"),
        std::make_tuple("wrl", "x-world/x-vrml"),
        std::make_tuple("wrz", "x-world/x-vrml"),
        std::make_tuple("xaf", "x-world/x-vrml"),
        std::make_tuple("xbm", "image/x-xbitmap"),
        std::make_tuple("xla", "application/vnd.ms-excel"),
        std::make_tuple("xlc", "application/vnd.ms-excel"),
        std::make_tuple("xlm", "application/vnd.ms-excel"),
        std::make_tuple("xls", "application/vnd.ms-excel"),
        std::make_tuple("xlt", "application/vnd.ms-excel"),
        std::make_tuple("xlw", "application/vnd.ms-excel"),
        std::make_tuple("xof", "x-world/x-vrml"),
        std::make_tuple("xpm", "image/x-xpixmap"),
        std::make_tuple("xwd", "image/x-xwindowdump"),
        std::make_tuple("z", "application/x-compress"),
        std::make_tuple("zip", "application/zip"),
    };

    std::string get_mime_from_ext(std::string ext) {
        static std::unordered_map<std::string, std::string> m;
        if (m.empty()) {
            for (auto& l : mime_list) {
                m.insert(std::make_pair(std::get<0>(l), std::get<1>(l)));
            }
        }
        if (ext.empty())
            return "application/octet-stream";
        auto f = m.find(ext);
        if (f != m.end()) {
            return f->second;
        }
        return "application/octet-stream";
    }
}